#######################################################
# The base classes for DEVS classes
# Author: moonho.hwang@gmail.com
#   Date: Oct.19.2011
#######################################################
from XSY.Base import *

class NamedObj:
   '''The base class for having a <i>name</i>. The name can be whatever such as a string, an integer, an float and so on.

   '''
   def __init__(self, n):
      '''constructor'''
      self.name = n
   
   def __str__(self): 
      '''convert itself to a string'''
      return self.name_to_s()

   def name_to_s(self):
      '''convert <i>name</i> to a string'''
      return self.name
#end of class NamedObj

def hiername(frm, upto=None):
   '''return hierarchical name delimiter '.' '''
   if frm == upto and frm: 
      return frm.name_to_s()
   else:
      if frm.parent != None:
         ancestor_str = hiername(frm.parent, upto)
         if ancestor_str != "":
            return ancestor_str + "." + frm.name_to_s()
         else:
            return frm.name_to_s()
      else: 
         return frm.name_to_s()
# end of hiername(from, upto)

class HierNamedObj(NamedObj):
   '''a class having a parent as well as name '''
   def __init__(self, n, parent=None):
      '''constructor '''
      NamedObj.__init__(self,n) 
      self.parent = parent
   
   def parent_hier_name_to_s(self):
      '''return a string of name if self has no parent 
      otherwise, the return string will be parent.name.to_s'''
      return hiername(self, self.parent)
# of class HierNamedObj

class TES(HierNamedObj):
   '''the class of timed event systems have a static data: tNow '''
   tNow = 0 # static variable to trace time now
   def __init__(self, n, parent=None):
      ''' constructor'''
      HierNamedObj.__init__(self, n, parent)
 
   def Delta_0(self, t):
      raise Exception("Method %s::%s must be overridden!"%(classname(self), __function__() ))

   def Delta_x(self, x, t):
      raise Exception("Method %s::%s must be overridden!"%(classname(self), __function__() ))
   
   def Delta_y(self, t):
      raise Exception("Method %s::%s must be overridden!"%(classname(self), __function__() ))

   def Delta_null(self, t):
      raise Exception("Method %s::%s must be overridden!"%(classname(self), __function__() ))

if __name__ == '__main__':
   print 'DEVSBase Test '
   seong = HierNamedObj("Seong")
   moon = HierNamedObj("Moon",seong)
   sun = HierNamedObj("Sun", moon)
   su = HierNamedObj("Su", sun)
   print hiername(su)
   print hiername(su,sun)
   print hiername(su,su.parent)
   print hiername(su,moon)

   Clock.tNow = 10

   print 'tnow=%.3f'%Clock.tNow

